<?php

declare(strict_types=1);


namespace App\Middleware;

use App\Utils\FunUtil;
use App\Utils\LoggerUtil;
use Hyperf\Context\Context;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AuthMiddleware implements MiddlewareInterface
{

    /**
     * @var RequestInterface
     */
    protected RequestInterface $request;

    /**
     * @var TranslatorInterface
     */
    protected TranslatorInterface $translator;

    public function __construct(RequestInterface $request, TranslatorInterface $translator)
    {
        $this->request = $request;
        $this->translator = $translator;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $params = $request->getParsedBody();
        $query = $request->getQueryParams();
        $params = array_merge($params, $query);
        $uid = $params['uid'] ?? 0;
        $sessionId = $params['session_id'] ?? '';
        $language = $params['language'] ?? '';
        $from = $params['from'] ?? 1;
        $version_code = $params['version_code'] ?? 0;
        $channel = $params['channel'] ?? '';
        if (!$this->unverifiedUri($this->request->getUri()->getPath(), $params)) {
            // 当前登录用户入参id
            Context::set('p_uid', $uid);
            $user = FunUtil::checkUser($uid, $sessionId, $language);
            Context::set('user', $user);
            if (empty($language) && isset($user['ext2']->language) && !empty($user['ext2']->language)) {
                $language = $user['ext2']->language;
            }
        }
        if ($language == '') {
            $language = 'en';
        }
        Context::set('language', $language);
        $this->translator->setLocale($language);
        Context::set('cvf', ['from' => $from, 'version_code' => $version_code, 'channel' => $channel]);

        // TODO: Implement process() method.
        return $handler->handle($request);
    }


    /**
     * Note - 符合参数时不需要登录接口
     * DateTime 2024/8/15 15:02
     * User Admin
     * @param $uri
     * @param $params
     * @return bool true表示不需要登录
     */
    private function unverifiedUri($uri, $params)
    {
        $unverifiedUri = [
            // 接口地址                                  接口参数名            接口参数对应的值
            ['uri' => '/app/v1/my/bind_user.php', 'param_name' => 'type', 'param_value' => 4],
            ['uri' => '/app/v1/test/test.php'],
            ['uri' => '/app/v1/user/login.php'],
            ['uri' => '/app/v1/user/session.php'],
            ['uri' => '/app/v1/send/captcha']
        ];
        foreach ($unverifiedUri as $subUri) {
            if ($uri == $subUri['uri']) {
                if (!isset($subUri['param_name']) || $subUri['param_value'] == '') {
                    return true;
                }
                if (isset($params[$subUri['param_name']]) && $params[$subUri['param_name']] == $subUri['param_value']) {
                    return true;
                }
            }
        }
        return false;
    }
}